<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="企业名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="企业状态"
          clearable
        >
          <el-option label="待审核" value="0" />
          <el-option label="已审核" value="1" />
          <el-option label="已拒绝" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:dept:add']"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-sort"
            size="mini"
            @click="toggleExpandAll"
            >展开/折叠</el-button
          >
        </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="status" label="序号" type="index">
      </el-table-column>
      <el-table-column prop="deptName" label="企业名称" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="code" label="统一社会信用代码"></el-table-column>

      <el-table-column prop="cateStr" label="所属类目" :show-overflow-tooltip="true" width="250"></el-table-column>

      <el-table-column prop="nature" label="公司性质"></el-table-column>

      <!-- <el-table-column
          prop="phone"
          label="手机号"
        ></el-table-column> -->
      <!-- <el-table-column prop="orderNum" label="排序" width="200"></el-table-column> -->
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.status == 0
                ? 'warning'
                : scope.row.status == 1
                ? 'success'
                : 'danger'
            "
            >{{
              scope.row.status == 0
                ? "待审核"
                : scope.row.status == 1
                ? "已审核"
                : "已拒绝"
            }}</el-tag
          >
          <!-- <dict-tag
              :options="dict.type.sys_normal_disable"
              :value="scope.row.status"
            /> -->
        </template>
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        class-name="small-padding fixed-width"
        width="160"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="primary"
            @click="handleUpdate(scope.row, 1)"
            >审核</el-button
          >
          
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="danger"
            @click="handleUpdate(scope.row, 2)"
            >拒绝</el-button
          >

          <!-- <el-button
              v-if="scope.row.parentId != 0"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:dept:remove']"
              >删除</el-button
            > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="70%"
      append-to-body
      :before-close="handleClose"
    >
      <addEnterprise
        :did="did"
        @cancel="open = false"
        @submitForm="submitForm"
        v-if="open"
      ></addEnterprise>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  listDeptApply,
  getDept,
  delDept,
  addDept,
  editApply,
  listDeptApplyExcludeChild,
} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import addEnterprise from "./addEnterprise.vue";

export default {
  name: "Dept",
//   dicts: ["sys_normal_disable"],
  components: { Treeselect, addEnterprise },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 企业树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      total: 0,
      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined,
        //   parentId: 1000002,
        pageSize: 10,
        pageNum: 1,
      },
      did: null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "上级企业不能为空", trigger: "blur" },
        ],
        deptName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "显示排序不能为空", trigger: "blur" },
        ],
        email: [
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
        phone: [
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // cancel()
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listDeptApply(this.queryParams).then((response) => {
        // console.log(response)
        this.total = response.total;
        this.deptList = response.rows;
        this.loading = false;
      });
    },
    /** 转换企业数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deptId: undefined,
        parentId: undefined,
        deptName: undefined,
        orderNum: undefined,
        leader: undefined,
        phone: undefined,
        email: undefined,
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.deptId;
      }
      this.did = null;
      this.open = true;
      this.title = "添加企业";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row, status) {
        // var 
        var tit = '审核'
        if(status == 2){
            tit = '拒绝'
        }
        this.$modal
        .confirm('是否确认'+tit+'?')
        .then(function () {
          return editApply({
            id:row.id,status:status,phone:row.phone
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function () {
      this.open = false;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.deptName + '"的数据项？')
        .then(function () {
          return delDept(row.deptId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
  